package com.ly.ocw.web.admin;

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ly.ocw.entity.weixin.WeixinCardTestwhitelist;
import com.ly.ocw.exception.ServerException;
import com.ly.ocw.service.weixin.WeixinCardService;
import com.ly.ocw.service.weixin.WeixinCardTestwhitelistService;
import com.ly.ocw.service.weixin.WeixinConf;

@Controller
@RequestMapping(value = "/admin/cardWhiteList")
public class CardWhitelListController {

	private static Logger logger = LoggerFactory.getLogger(CardWhitelListController.class);

	@Autowired
	private WeixinCardTestwhitelistService weixinCardTestwhitelistService;
	
	@Autowired
	private WeixinCardService weixinCardService;
	
	@Autowired
	private WeixinConf weixinConf;
	

	//http://wx.wonderlink.com.cn:80/boycard/admin/card/list
	// to card list
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		
		List<WeixinCardTestwhitelist> whitelList = weixinCardTestwhitelistService.getAllByWeixinPublicId(weixinConf.getPublicId());
		
		logger.info("cardWhiteList/list size :"+whitelList.size());
		
		model.addAttribute("whitelList", whitelList);
		
		return "admin/card/cardTestWhiteList";
	}
	
	// to card list
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String toCreatePage(Model model) {
		logger.info("toCreatePage");
		return "admin/card/cardTestWhiteForm";
	}
	
	// to card list
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(@ModelAttribute("weixinCardTestwhitelist") WeixinCardTestwhitelist weixinCardTestwhitelist,Model model, RedirectAttributes redirectAttributes) throws ClientProtocolException, IOException, ServerException {
		logger.info("save WeixinCardTestwhitelist");
		String result = weixinCardService.addTestWhiteList(weixinCardTestwhitelist);
		redirectAttributes.addFlashAttribute("message", result);
		return "redirect:/admin/cardWhiteList/list";
	}

}
